package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import u.aly.da;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.HappyBiExchangeRecordAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.ExchangeRecordData;
import com.kkd.kkdapp.http.entity.ExchangeRecordItem;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.Time_Date;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

public class HappyBiRecordActivity extends BaseActivity implements
		OnClickListener, KKDScrollListener {

	private final int CODE_ZERO = 1;
	private final int CODE_ELSE = 2;

	private Context mContext = this;
	private RelativeLayout happyBiRecord_top_img_back;
	private KkdXlistView happyBiRecord_lv;
	private ListView mListView;
	private HappyBiExchangeRecordAdapter adapter;
	private int pageNO = 1;
	private int pageSize = 10;
	private int pageCount;
	private List<ExchangeRecordItem> totalList = new ArrayList<ExchangeRecordItem>();
	private ImageView image_null;
	private TextView tv_null;
	private boolean REFRESHFLAG = true;
	private ExchangeRecordData data;

	private Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case CODE_ZERO:

				int PageSize = data.getPageSize();
				int totalCount = data.getTotalCount();
				if (totalCount % PageSize != 0) {
					if (totalCount == 0) {
						pageCount = 0;
					} else {
						pageCount = (totalCount / PageSize) + 1;
					}
				} else {
					pageCount = (totalCount / PageSize);
				}
				if (pageNO > pageCount) {
					happyBiRecord_lv.stopLoadMore(-1);
					if (REFRESHFLAG) {
						happyBiRecord_lv.stopLoadMore(1);
					}
				} else {
					happyBiRecord_lv.setNoMore(false);
					if (pageNO == 1) {
						totalList.clear();
					}
					totalList.addAll(data.getDataList());
					adapter.notifyDataSetChanged();
					if (data.getDataList().size() < pageSize) {
						happyBiRecord_lv.stopLoadMore(1);
					} else {
						happyBiRecord_lv.stopLoadMore(0);
					}
					happyBiRecord_lv.stopRefresh();
					Time_Date time_Date = new Time_Date();
					String time = time_Date.get_Year_Day();
					happyBiRecord_lv.setRefreshTime(time);
				}
				happyBiRecord_lv.stopRefresh();
				Time_Date time_Date = new Time_Date();
				String time = time_Date.get_Year_Day();
				happyBiRecord_lv.setRefreshTime(time);
				if (REFRESHFLAG) {
					if (totalList == null || totalList.size() == 0) {
						image_null.setVisibility(View.VISIBLE);
						tv_null.setVisibility(View.VISIBLE);
						tv_null.setText("暂无兑换记录，赶快去兑换吧~");
						image_null.setImageResource(R.drawable.img_null);
					} else {
						image_null.setVisibility(View.GONE);
						tv_null.setVisibility(View.GONE);
					}
				}

				break;
			case CODE_ELSE:
				happyBiRecord_lv.stopLoadMore(1);
				happyBiRecord_lv.stopRefresh();
				break;
			}

		};

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_happy_bi_record);
		initView();
		setListener();
		initData();
	}

	private void initData() {
		if (AppUtil.isConnect(mContext)) {
			// image_null.setVisibility(View.GONE);
			// tv_null.setVisibility(View.GONE);
			requestExchangeRecord();
		} else {
			if (totalList == null || totalList.size() == 0) {
				image_null.setVisibility(View.VISIBLE);
				tv_null.setVisibility(View.VISIBLE);
				tv_null.setText("网络连接失败，请检查网络！");
				image_null.setImageResource(R.drawable.img_null);
				happyBiRecord_lv.stopRefresh();
			} else {
				showToast("网络连接失败");
				happyBiRecord_lv.stopLoadMore(0);
				happyBiRecord_lv.stopRefresh();
			}
		}
	}

	private void initView() {
		happyBiRecord_top_img_back = (RelativeLayout) findViewById(R.id.happyBiRecord_top_img_back);
		happyBiRecord_lv = (KkdXlistView) findViewById(R.id.happyBiRecord_lv);
		image_null = (ImageView) findViewById(R.id.id_no_content);
		tv_null = (TextView) findViewById(R.id.id_no_content_text);
		happyBiRecord_lv.setKKDScrollListener(this);
		mListView = happyBiRecord_lv.mListView;
		mListView.setDividerHeight(0);
		adapter = new HappyBiExchangeRecordAdapter(mContext, totalList);
		mListView.setAdapter(adapter);
		happyBiRecord_lv.startRefresh();
	}

	private void setListener() {
		happyBiRecord_top_img_back.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.happyBiRecord_top_img_back:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		REFRESHFLAG = true;
		initData();
	}

	@Override
	public void onLoadMore() {
		pageNO++;
		REFRESHFLAG = false;
		requestExchangeRecord();
	}

	private void requestExchangeRecord() {
		KkdHttpMagager.getInstance().coinExchangeRecord( pageNO,
				pageSize,
				ExchangeRecordData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						data = (ExchangeRecordData) responseBody;
						handler.sendEmptyMessage(CODE_ZERO);

					}

					@Override
					public void onFailure(int statusCode, String desc) {

						handler.sendEmptyMessage(CODE_ELSE);

					}
				});
	}
}
